﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using DevExpress.XtraReports.UI;
using System.Data;
using DevTeam;

namespace ConfExpert.Forms.Rapor.Aksesuar
{
    public partial class AksesuarCikisTalepFormu : DevReport
    {
        DataSet dsData;
        public AksesuarCikisTalepFormu(string AksesuarCikisTalepleri,bool IsPreview)
        {
            InitializeComponent();
            dsData = ExecuteDataSet("GetAksCikisForm", AksesuarCikisTalepleri);

            Watermark.Text = IsPreview ? "Önizleme" : "";

            tbAciklama.PrintOnPage += tbAciklama_PrintOnPage;

            xrSubAksesuarCikisTalepRaporu xrSubReport = (xrSubAksesuarCikisTalepRaporu)xrSub.ReportSource;
            xrSubReport.fillTable(dsData.Tables["Table"]);

            BindData();
        }

        #region BindData
        private void BindData()
        {
            this.DataMember = "Table1";
            this.DataSource = dsData;

            //Header bilgilerini bağlıyoruz
            DataRow drHeader = dsData.Tables[0].Rows[0];
            tcAtolye.Text = drHeader["Atelye"].ToStr();

            //Detay bilgilerini bağlıyoruz
            tcStokKod.DataBindings.Add("Text", null, "Table1.StokKod");
            tcMarka.DataBindings.Add("Text", null, "Table1.Marka");
            tcMalzeme.DataBindings.Add("Text", null, "Table1.Malzeme");
            tcOzellik.DataBindings.Add("Text", null, "Table1.Ozellik");
            tcBirimAdet.DataBindings.Add("Text", null, "Table1.BirimAdet", "{0:#,##0.###}");
            tcCikilabilir.DataBindings.Add("Text", null, "Table1.Cikilabilir", "{0:#,#}");
            tcQty.DataBindings.Add("Text", null, "Table1.Qty", "{0:#,#}");

            if (dsData.Tables.Count > 2)
            {
                string[] Dosyalar = dsData.Tables[2].Rows[0]["DosyaNo"].ToStr().Replace("[", "").Replace("]", "").Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

                xrTable3.BeginInit();
                xrTable9.BeginInit();

                foreach (string DosyaNo in Dosyalar)
                {
                    float Width = 30;
                    XRTableCell tcHeader = new XRTableCell();
                    tcHeader.WidthF = Width;
                    tcHeader.Borders = ((DevExpress.XtraPrinting.BorderSide)(((DevExpress.XtraPrinting.BorderSide.Right | DevExpress.XtraPrinting.BorderSide.Top) | DevExpress.XtraPrinting.BorderSide.Bottom)));
                    tcHeader.Text = DosyaNo;
                    tcHeader.Angle = 90;
                    trHeader.Cells.Add(tcHeader);

                    XRTableCell tcDetail = new XRTableCell();
                    tcDetail.WidthF = Width;
                    tcDetail.Borders = ((DevExpress.XtraPrinting.BorderSide)((DevExpress.XtraPrinting.BorderSide.Right | DevExpress.XtraPrinting.BorderSide.Bottom)));
                    tcDetail.Font = new System.Drawing.Font("Calibri", 8F, System.Drawing.FontStyle.Regular);
                    tcDetail.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleRight;
                    tcDetail.Padding = new DevExpress.XtraPrinting.PaddingInfo(0, 5, 0, 0, 100F);
                    tcDetail.DataBindings.Add("Text", null, "Table1." + DosyaNo, "{0:#,#}");
                    trDetail.Cells.Add(tcDetail);
                    tcDetail.BeforePrint += ReSize;
                }

                bool BoyamaVarmi = dsData.Tables["Table1"].Select("Boyama = 'B'").Length > 0;

                if (BoyamaVarmi)
                {
                    float Width = 20;
                    XRTableCell tcHeader = new XRTableCell();
                    tcHeader.WidthF = Width;
                    tcHeader.Borders = ((DevExpress.XtraPrinting.BorderSide)(((DevExpress.XtraPrinting.BorderSide.Right | DevExpress.XtraPrinting.BorderSide.Top) | DevExpress.XtraPrinting.BorderSide.Bottom)));
                    trHeader.Cells.Add(tcHeader);

                    XRTableCell tcDetail = new XRTableCell();
                    tcDetail.WidthF = Width;
                    tcDetail.Borders = ((DevExpress.XtraPrinting.BorderSide)((DevExpress.XtraPrinting.BorderSide.Right | DevExpress.XtraPrinting.BorderSide.Bottom)));
                    tcDetail.Font = new System.Drawing.Font("Calibri", 9F, System.Drawing.FontStyle.Regular);
                    tcDetail.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleRight;
                    tcDetail.Padding = new DevExpress.XtraPrinting.PaddingInfo(0, 5, 0, 0, 100F);
                    tcDetail.DataBindings.Add("Text", null, "Table1.Boyama");
                    trDetail.Cells.Add(tcDetail);
                }

                trHeader.WidthF = trDetail.WidthF = 2800;
                xrTable3.WidthF = xrTable9.WidthF = 2800;

                tcQty.WidthF = tcCikilabilir.WidthF = xrTableCell5.WidthF = xrTableCell8.WidthF = 125;

                xrTable3.EndInit();
                xrTable9.EndInit();

                //tcStokKod.BeforePrint += ReSize;
                //tcMarka.BeforePrint += ReSize;
                //tcMalzeme.BeforePrint += ReSize;
                //tcOzellik.BeforePrint += ReSize;
                //tcBirimAdet.BeforePrint += ReSize;
                tcCikilabilir.BeforePrint += ReSize;
                tcQty.BeforePrint += ReSize;
                
            }
        }

        #endregion

        void tbAciklama_PrintOnPage(object sender, PrintOnPageEventArgs e)
        {
            bool BoyamaVarmi = dsData.Tables["Table1"].Select("Boyama = 'B'").Length > 0;

            if (e.PageCount > 0)
                if (e.PageIndex != e.PageCount - 1 || !BoyamaVarmi)
                    e.Cancel = true;
        }
        private void ReSize(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            //Resize ı düzgün yapan tek fonksiyon !!!
            Graphics gr = Graphics.FromHwnd(IntPtr.Zero);
            gr.PageUnit = GraphicsUnit.Millimeter;
            int factor = 11;

            XRTableCell label = (XRTableCell)sender;

            string tcName = ((XRTableCell)sender).Name;
            FontStyle fs = tcName == "tcCikilabilir" ? FontStyle.Bold : FontStyle.Regular;

            float newSize = 8f;
            Font defaultFont = new System.Drawing.Font("Calibri", newSize, fs);

            float stringWidth = gr.MeasureString(label.Text, defaultFont).Width;
            float labelWidth = label.WidthF;


            if (labelWidth < stringWidth * factor)
                newSize = defaultFont.Size * label.WidthF / stringWidth / factor;


            label.Font = new Font(defaultFont.FontFamily, newSize,fs);
            gr.Dispose();
        }

    }
}
